package com.lill.org.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lill.common.component.util.IdUtil;
import com.lill.org.constant.ArchitectureType;
import com.lill.org.constant.OrgConstant;
import com.lill.org.dao.ICompanyArchitectureDao;
import com.lill.org.dao.ICompanyInfoDao;
import com.lill.org.dao.IPositionDictDao;
import com.lill.org.model.CompanyArchitectureModel;
import com.lill.org.model.CompanyInfoModel;
import com.lill.org.model.EmployeeModel;
import com.lill.org.model.PositionDictModel;
import com.lill.org.service.ICompanyInfoService;
import com.lill.org.service.IEmployeeService;

/**
 * 公司管理Service实现类
 *
 * @author lill
 * @date 2019-04-24 15:25:17
 */
@Service("com.lill.org.service.impl.CompanyInfoServiceImpl")
public class CompanyInfoServiceImpl implements ICompanyInfoService {
	
    @Autowired
    private ICompanyInfoDao companyInfoDao;
    @Autowired
    private ICompanyArchitectureDao architectureDao;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IPositionDictDao positionDictDao;

    public CompanyInfoModel queryObject(String id) {
    	CompanyInfoModel companyInfo=companyInfoDao.queryObject(id);
        return companyInfo;
    }

    public List<CompanyInfoModel> queryList(Map<String, Object> map) {
        return companyInfoDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return companyInfoDao.queryTotal(map);
    }

    @Transactional
    public int saveOther(CompanyInfoModel orgCompanyInfo,String id ){
        orgCompanyInfo.setId(id);

        CompanyArchitectureModel architecture=new CompanyArchitectureModel();
        architecture.setId(id);
        architecture.setParentId(OrgConstant.ROOT);
        architecture.setParentList(id);
        architecture.setType(ArchitectureType.ROOT.getVal());
        architecture.setNum(0);
        architectureDao.save(architecture);

        EmployeeModel employee=new EmployeeModel();
        employee.setCompanyId(id);
        employee.setDepartId(id);
        if(StringUtils.isEmpty(orgCompanyInfo.getPersonId())){
        	employee.setName(orgCompanyInfo.getAttn());
        	employee.setPosition(orgCompanyInfo.getPosition());
        	employee.setPhone(orgCompanyInfo.getPhone());
        }else{
        	employee.setPersonId(orgCompanyInfo.getPersonId());
        }
        employee.setCreateTime(orgCompanyInfo.getCreateTime());
        employee.setCreateUser(orgCompanyInfo.getCreateUser());
        employee.setStatus(1);

        EmployeeModel backEmployee=employeeService.saveBackModel(employee);
        orgCompanyInfo.setPersonId(backEmployee.getPersonId());
        
//        PositionType[] positionlist=PositionType.values();
        List<Map<String, Object>> positionlist=positionDictDao.queryDictList();
        PositionDictModel dictModel=null;
        for (int i = 0; i < positionlist.size(); i++) {
            dictModel=new PositionDictModel();
            dictModel.setId(IdUtil.createIdbyUUID());
            dictModel.setCompanyId(id);
            dictModel.setName(positionlist.get(i).get("name").toString());
            dictModel.setCode(positionlist.get(i).get("id").toString());
            positionDictDao.save(dictModel);
        }
        return companyInfoDao.save(orgCompanyInfo);
    }

    @Transactional
    public int save(CompanyInfoModel orgCompanyInfo) {
    	String id=IdUtil.createIdBy16UUID();
    	return this.saveOther(orgCompanyInfo,id);
    }

    public int update(CompanyInfoModel orgCompanyInfo) {
        return companyInfoDao.update(orgCompanyInfo);
    }

    public int delete(String id) {
        return companyInfoDao.delete(id);
    }

    public int deleteBatch(String[] ids) {
        return companyInfoDao.deleteBatch(ids);
    }
    
	public int deleteLogicByMap(Map<String, Object> map) {
		return companyInfoDao.deleteLogicByMap(map);
	}

	@Transactional
	public int freezeLogicByMap(Map<String, Object> params) {
		return companyInfoDao.updateCompanyStatusByMap(params);
	}

    @Override
    public Map<String, Object> queryThemeColor(Map<String, Object> params) {
        return companyInfoDao.queryThemeColor(params);
    }
}
